home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Graphics / sKulpt / skulpt-src / File-Bin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-28  |  11.0 KB  |  378 lines

  1. #define STRICT
  2.  
  3. // Includes standard Windows
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <memory.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <io.h>
  13.  
  14. // Includes D3D
  15. #define  D3D_OVERLOADS
  16. #include <ddraw.h>
  17. #include <d3d.h>
  18. #include <d3dx.h>
  19.  
  20. // Includes utilitaires D3D
  21. #include "d3dmath.h"
  22. #include "d3dutil.h"
  23. #include "D3DEnum.h"
  24.  
  25. // Ids Resources
  26. #include "resource.h"
  27.  
  28. // Constantes
  29. #include "const.h"
  30.  
  31. // Types
  32. #include "types.h"
  33.  
  34. // Variables globales projet
  35. #include "vars.h"
  36.  
  37. // Prototypes fonctions autres modules
  38. #include "proto.h"
  39.  
  40. // Macros
  41. #include "macros.h"
  42.  
  43. void vSaveBin(void)
  44. {
  45.     char cBuffer[13];
  46.     static TCHAR sFileName[512];
  47.     FILE* hFile;
  48.  
  49. #ifndef _AMIGA_
  50.     static TCHAR sInitialDir[512] = "";
  51.     TCHAR sCurrentName[512] = "*.sk3d";
  52.  
  53.     // Récupérer le directory courant
  54.     GetCurrentDirectory(sizeof(sInitialDir), sInitialDir);
  55.  
  56.     // Préparer une structure OPENFILENAME
  57.     OPENFILENAME sOFName = { sizeof(OPENFILENAME), NULL, NULL,
  58.                          "Fichiers Scènes sKulpt (*.sk3d)\0*.sk3d\0\0",
  59.                          NULL, 0, 1, sCurrentName, 512, sFileName, 512,
  60.                          sInitialDir, "Ouvrir un fichier scène sKulpt", 0, 0, 1,
  61.                          ".sk3d", 0, NULL, NULL };
  62.  
  63.     // Choisir un fichier
  64.     if(!GetOpenFileName(&sOFName))
  65.         return;
  66.  
  67.     // Stocker le nom du directory pour le prochain appel
  68.     strcpy(sInitialDir, sCurrentName);
  69.     strstr(sInitialDir, sFileName)[0] = '\0';
  70. #else
  71.     strcpy(sFileName, "#?.sk3d");
  72.     if (!FSelect("Ouvrir...", sFileName))
  73.         return;
  74. #endif
  75.  
  76.     hFile = fopen(sFileName,
  77. #ifndef _AMIGA_
  78.                      "wb");
  79. #else
  80.                      "w");
  81. #endif
  82.  
  83.     if (!hFile)
  84.     {
  85.         vTrace("Pb ouverture fichier %s", sFileName);
  86.         return;
  87.     }
  88.  
  89.     vTrace("*** Fichier scène ouvert : %s", sFileName);
  90.         
  91.     // Garbage collecter
  92.     vCollect();
  93.  
  94.     // Ecrire la structure d'entête et les données sKulpt 3D mode binaire
  95.     strcpy(cBuffer,
  96. #ifndef _AMIGA_
  97.                     "FORMINTLSK3D"
  98. #else
  99.                     "FORMM68KSK3D"
  100. #endif
  101.            );
  102.  
  103.     fwrite(cBuffer, 1, 12, hFile);
  104.  
  105.     vTrace("Ecriture de %d vertices", iVertFirstAvailable);
  106.     fwrite("VERT", 1, 4, hFile);
  107.     fwrite(&iVertFirstAvailable, 1, sizeof(iVertFirstAvailable), hFile);
  108.     fwrite(Vertices, 1, iVertFirstAvailable * sizeof(gSommet), hFile);
  109.  
  110.     vTrace("Ecriture de %d lampes", iLampFirstAvailable);
  111.     fwrite("LAMP", 1, 4, hFile);
  112.     fwrite(&iLampFirstAvailable, 1, sizeof(iLampFirstAvailable), hFile);
  113.     fwrite(Lampes, 1, iLampFirstAvailable * sizeof(gLamp), hFile);
  114.  
  115.     vTrace("Ecriture de %d triangles", iTriaFirstAvailable);
  116.     fwrite("FACE", 1, 4, hFile);
  117.     fwrite(&iTriaFirstAvailable, 1, sizeof(iTriaFirstAvailable), hFile);
  118.     fwrite(Triangles, 1, iTriaFirstAvailable * sizeof(gTri), hFile);
  119.  
  120.     vTrace("Ecriture de %d arêtes", iEdgeFirstAvailable);
  121.     fwrite("EDGE", 1, 4, hFile);
  122.     fwrite(&iEdgeFirstAvailable, 1, sizeof(iEdgeFirstAvailable), hFile);
  123.     fwrite(Edges, 1, iEdgeFirstAvailable * sizeof(gEdge), hFile);
  124.  
  125.     vTrace("Ecriture de %d matériaux", iMtrlFirstAvailable);
  126.     fwrite("MTRL", 1, 4, hFile);
  127.     fwrite(&iMtrlFirstAvailable, 1, sizeof(iMtrlFirstAvailable), hFile);
  128.     fwrite(Materials, 1, iMtrlFirstAvailable * sizeof(gMtrl), hFile);
  129.  
  130.     vTrace("Ecriture de l'environnement");
  131.     fwrite("OBSV", 1, 4, hFile);
  132.     fwrite(&Observer, 1, sizeof(Observer), hFile);
  133.     fwrite(&Target, 1, sizeof(Target), hFile);
  134.     fwrite(&cAmbient, 1, sizeof(cAmbient), hFile);
  135.     fwrite(&cBack, 1, sizeof(cBack), hFile);
  136.  
  137.     // Fermer le fichier
  138.     fclose(hFile);
  139.     vTrace("Fichier scène créé");
  140. }
  141.  
  142. void vLoadBin(void)
  143. {
  144.     int iNumVertex = 0, iNumFace = 0, iNumLamps = 0, iNumEdges = 0, iNumMtrl = 0, iX;
  145.     char cBuffer[13];
  146.     DWORD dwNBytesRead;
  147.     static TCHAR sFileName[512];
  148.  
  149. #ifndef _AMIGA_
  150.     HANDLE hFile;
  151.     static TCHAR sInitialDir[512] = "";
  152.     TCHAR sCurrentName[512] = "*.sk3d";
  153.     OFSTRUCT ReOpenBuff;
  154.     // Récupérer le directory courant
  155.     GetCurrentDirectory(sizeof(sInitialDir), sInitialDir);
  156.  
  157.     // Préparer une structure OPENFILENAME
  158.     OPENFILENAME sOFName = { sizeof(OPENFILENAME), NULL, NULL,
  159.                          "Fichiers Scènes sk3d (*.sk3d)\0*.sk3d\0\0",
  160.                          NULL, 0, 1, sCurrentName, 512, sFileName, 512,
  161.                          sInitialDir, "Ouvrir un fichier scène sk3d", OFN_FILEMUSTEXIST, 0, 1,
  162.                          ".sk3d", 0, NULL, NULL };
  163.  
  164.     // Choisir un fichier
  165.     if(!GetOpenFileName(&sOFName))
  166.         return;
  167.  
  168.     // Stocker le nom du directory pour le prochain appel
  169.     strcpy(sInitialDir, sCurrentName);
  170.     strstr(sInitialDir, sFileName)[0] = '\0';
  171.  
  172.     // Ouvrir le fichier en lecture
  173.     if ((HANDLE) HFILE_ERROR == (hFile = (HANDLE) OpenFile(sFileName, &ReOpenBuff, OF_READ)))
  174.         return;
  175. #else
  176.     FILE* hFile;
  177.  
  178.     strcpy(sFileName, "#?.sk3d");
  179.  
  180.     if (!FSelect("Ouvrir...", sFileName))
  181.         return;
  182.  
  183.     hFile = fopen(sFileName, "r");
  184.     if (!hFile)
  185.     {
  186.         vTrace("Pb ouverture fichier %s", sFileName);
  187.         return;
  188.     }
  189. #endif // _AMIGA_
  190.  
  191.     vTrace("*** Fichier scène ouvert :");
  192.     vTrace(sFileName);
  193.         
  194.     // Effacer tous les objets présents en mémoire
  195.     vDeleteObjects();
  196.  
  197.     // Vérifier la structure d'entête SKulpt 3D mode binaire
  198. #ifndef _AMIGA_
  199.     ReadFile(hFile, cBuffer, 12, &dwNBytesRead, NULL);
  200. #else
  201.     dwNBytesRead = fread(cBuffer, 1, 12, hFile);
  202. #endif
  203.  
  204.     if (strncmp((char *) cBuffer,
  205. #ifndef _AMIGA_
  206.          "FORMINTLSK3D"
  207. #else
  208.          "FORMM68KSK3D"
  209. #endif
  210.          , 12))
  211.     {
  212.         vTrace("*** E0024 : En-tête fichier scène incorrect");
  213.         goto _Fin;
  214.     }
  215.  
  216.     // Parcourir le fichier en décodant les chunks au fur et à mesure
  217.     cBuffer[4] = 0;
  218. #ifndef _AMIGA_
  219.     while ((ReadFile(hFile, cBuffer, 4, &dwNBytesRead, NULL)) && (dwNBytesRead == 4))
  220. #else
  221.     while (4 == (dwNBytesRead = fread(cBuffer, 1, 4, hFile)))
  222. #endif
  223.     {
  224.         vTrace("Chunk : %4s", cBuffer);
  225.  
  226.         if (!strncmp((char *) cBuffer, "VERT", 4))  // Sommets
  227.         {
  228.             // Charger le nombre de vertex
  229. #ifndef _AMIGA_
  230.             ReadFile(hFile, &iNumVertex, 4, &dwNBytesRead, NULL);
  231. #else
  232.             dwNBytesRead = fread(&iNumVertex, 1, 4, hFile);
  233. #endif
  234.             vTrace("Nombre de sommets : %ld", iNumVertex);
  235.  
  236.             // Lire directement le chunk vertices
  237. #ifndef _AMIGA_
  238.             ReadFile(hFile, Vertices, iNumVertex * sizeof(gSommet), &dwNBytesRead, NULL);
  239. #else
  240.             dwNBytesRead = fread(Vertices, 1, iNumVertex * sizeof(gSommet), hFile);
  241. #endif
  242.             continue;
  243.         }
  244.  
  245.         if (!strncmp((char *) cBuffer, "LAMP", 4))  // Lampes
  246.         {
  247.             // Charger le nombre de lampes
  248. #ifndef _AMIGA_
  249.             ReadFile(hFile, &iNumLamps, 4, &dwNBytesRead, NULL);
  250. #else
  251.             dwNBytesRead = fread(&iNumLamps, 1, 4, hFile);
  252. #endif
  253.             vTrace("Nombre de lampes : %ld", iNumLamps);
  254.  
  255.             // Lire les lampes
  256. #ifndef _AMIGA_
  257.             ReadFile(hFile, Lampes, iNumLamps * sizeof(gLamp), &dwNBytesRead, NULL);
  258. #else
  259.             dwNBytesRead = fread(Lampes, 1, iNumLamps * sizeof(gLamp), hFile);
  260. #endif
  261.             continue;
  262.         }
  263.  
  264.         if (!strncmp((char *) cBuffer, "FACE", 4)) // Triangles
  265.         {
  266.             // Charger le nombre de faces
  267. #ifndef _AMIGA_
  268.             ReadFile(hFile, &iNumFace, 4, &dwNBytesRead, NULL);
  269. #else
  270.             dwNBytesRead = fread(&iNumFace, 1, 4, hFile);
  271. #endif
  272.             vTrace("Nombre de faces : %ld", iNumFace);
  273.  
  274.             // Lire les faces
  275. #ifndef _AMIGA_
  276.             ReadFile(hFile, Triangles, iNumFace * sizeof(gTri), &dwNBytesRead, NULL);
  277. #else
  278.             dwNBytesRead = fread(Triangles, 1, iNumFace * sizeof(gTri), hFile);
  279. #endif
  280.             continue;
  281.         }
  282.  
  283.         if (!strncmp((char *) cBuffer, "EDGE", 4)) // Arêtes
  284.         {
  285.             // Charger le nombre d'arêtes
  286. #ifndef _AMIGA_
  287.             ReadFile(hFile, &iNumEdges, 4, &dwNBytesRead, NULL);
  288. #else
  289.             dwNBytesRead = fread(&iNumEdges, 1, 4, hFile);
  290. #endif
  291.             vTrace("Nombre d'arêtes : %ld", iNumEdges);
  292.  
  293.             // Lire les arêtes
  294. #ifndef _AMIGA_
  295.             ReadFile(hFile, Edges, iNumEdges * sizeof(gEdge), &dwNBytesRead, NULL);
  296. #else
  297.             dwNBytesRead = fread(Edges, 1, iNumEdges * sizeof(gEdge), hFile);
  298. #endif
  299.             continue;
  300.         }
  301.  
  302.         if (!strncmp((char *) cBuffer, "MTRL", 4)) // Matériaux
  303.         {
  304.             // Charger le nombre de matériaux
  305. #ifndef _AMIGA_
  306.             ReadFile(hFile, &iNumMtrl, 4, &dwNBytesRead, NULL);
  307. #else
  308.             dwNBytesRead = fread(&iNumMtrl, 1, 4, hFile);
  309. #endif
  310.             vTrace("Nombre de matériaux : %ld", iNumMtrl);
  311.  
  312.             // Lire les arêtes
  313. #ifndef _AMIGA_
  314.             ReadFile(hFile, Materials, iNumMtrl * sizeof(gMtrl), &dwNBytesRead, NULL);
  315. #else
  316.             dwNBytesRead = fread(Materials, 1, iNumMtrl * sizeof(gMtrl), hFile);
  317. #endif
  318.             continue;
  319.         }
  320.  
  321.         if (!strncmp((char *) cBuffer, "OBSV", 4)) // Environnement
  322.         {
  323.             // Charger la structure observer
  324. #ifndef _AMIGA_
  325.             ReadFile(hFile, &Observer, sizeof(Observer), &dwNBytesRead, NULL);
  326.             ReadFile(hFile, &Target, sizeof(Observer), &dwNBytesRead, NULL);
  327.             ReadFile(hFile, &cAmbient, sizeof(cAmbient), &dwNBytesRead, NULL);
  328.             ReadFile(hFile, &cBack, sizeof(cBack), &dwNBytesRead, NULL);
  329. #else
  330.             dwNBytesRead = fread(&Observer, 1, sizeof(Observer), hFile);
  331.             dwNBytesRead = fread(&Target, 1, sizeof(Target), hFile);
  332.             dwNBytesRead = fread(&cAmbient, 1, sizeof(cAmbient), hFile);
  333.             dwNBytesRead = fread(&cBack, 1, sizeof(cBack), hFile);
  334. #endif
  335.             D3DUtil_SetViewMatrix(matView,
  336.               Observer, // From
  337.               Target,   // To
  338.               D3DVECTOR(0.f, 0.f, 0.f));
  339.             continue;
  340.         }
  341.  
  342.         // Chunk non traité.
  343. #ifndef _AMIGA_
  344.         ReadFile(hFile, &iX, 4, &dwNBytesRead, NULL);
  345. #else
  346.         dwNBytesRead = fread(&iX, 1, 4, hFile);
  347. #endif
  348.  
  349.         vTrace("Skip %d octets", iX);
  350. #ifndef _AMIGA_
  351.         SetFilePointer(hFile, iX, NULL, FILE_CURRENT);
  352. #else
  353.         fseek(hFile, iX, SEEK_CUR);
  354. #endif
  355.     }
  356.  
  357. _Fin:
  358.  
  359.     // Fermer le fichier
  360. #ifndef _AMIGA_
  361.     CloseHandle(hFile);
  362. #else
  363.     fclose(hFile);
  364. #endif
  365.  
  366.     iVertLastUsed = (iVertFirstAvailable = iNumVertex) - 1;
  367.     iEdgeLastUsed = (iEdgeFirstAvailable = iNumEdges) - 1;
  368.     iLampLastUsed = (iLampFirstAvailable = iNumLamps) - 1;
  369.     iTriaLastUsed = (iTriaFirstAvailable = iNumFace) - 1;
  370.     iMtrlLastUsed = (iMtrlFirstAvailable = iNumMtrl) - 1;
  371.  
  372. #ifndef _AMIGA_
  373.     // Forcer le recalcul des sommets de chaque triangle (car on a changé les points)
  374.     for (int iTriangle = 0 ; iTriangle <= iTriaLastUsed ; iTriangle++)
  375.         bUpdateD3DTri(iTriangle);
  376. #endif
  377. }
  378.